<?php

include_once "../../models/core.php";

include_once "../../models/auth.php";

include_once "../../controllers/office/loginClass.php";

session_start();


$username = $_POST["username"];
$password = $_POST["password"];

$login = new Login();

// 1. 也是可以通过下面的 getUser 函数传递参数 $avatarPath 的方式，获取这个用户的头像 avatar 路径
$avatarPath = null; 
// 2. 也是可以通过 getUser 函数的返回值的方式返回的
$result = $login->getUser($conn, $username, $password);



if($result["status"]){
    $uid = $result["msg"]["uid"]; 
    $token = $result["msg"]["token"];
    // $_SESSION[$uid] = $token; // error
    $_SESSION["u{$uid}"] = $token; // 假设用户 uid = 1， 那么 ssession 键则为 u1

    // echo "<script>alert('$uid');</script>";
    // echo "<script>setTimeout(() => {window.location.href = '../../views/user/person.html?uid={$uid}&token={$token}&refer=ax.com';}, 1000);</script>";
    
    // 我们的通过 getUser 函数返回值的方式返回用户的 avatar 头像的路径
    echo "<script>window.location.href = '../../views/user/person.html?uid={$uid}&token={$token}&refer=ax.com&avatarPath={$result["avatarPath"]}';</script>";
}else{
    echo "<script>alert('登陆失败！！！'); window.location.href = '../../views/office/login.html'</script>";
}


